import { ethers } from "ethers";

const timeStamp2String = (timestamp) => {
  // time=>时间戳、 type=>转换类型
  const date = new Date(timestamp * 1000); // 将10位时间戳转换为13位时间戳
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, "0");
  const day = date.getDate().toString().padStart(2, "0");
  const hours = date.getHours().toString().padStart(2, "0");
  const minutes = date.getMinutes().toString().padStart(2, "0");
  const seconds = date.getSeconds().toString().padStart(2, "0");
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};

// 转时间戳
const formatTimeFromTimestamp = (timestamp) => {
  const secondsAgo = Math.floor(Date.now() / 1000 - timestamp);
  if (timestamp === 0) {
    return "0:0:0"; // 时间戳为当前时间
  } else if (secondsAgo < 60) {
    if (localStorage.getItem("lang") == "zh-Hans") {
      return secondsAgo + "秒前";
    } else if (localStorage.getItem("lang") == "zh-Hant") {
      return secondsAgo + "秒前";
    } else {
      return secondsAgo + "Seconds ago";
    }
  } else if (secondsAgo < 3600) {
    if (localStorage.getItem("lang") == "zh-Hans") {
      return Math.floor(secondsAgo / 60) + "分钟前";
    } else if (localStorage.getItem("lang") == "zh-Hant") {
      return Math.floor(secondsAgo / 60) + "分鐘前";
    } else {
      return Math.floor(secondsAgo / 60) + "Minutes ago";
    }
  } else if (secondsAgo < 86400) {
    if (localStorage.getItem("lang") == "zh-Hans") {
      return Math.floor(secondsAgo / 3600) + "小时前";
    } else if (localStorage.getItem("lang") == "zh-Hant") {
      return Math.floor(secondsAgo / 3600) + "小時前";
    } else {
      return Math.floor(secondsAgo / 3600) + "Hours ago";
    }
  } else if (secondsAgo < 2592000) {
    if (localStorage.getItem("lang") == "zh-Hans") {
      return Math.floor(secondsAgo / 86400) + "天前";
    } else if (localStorage.getItem("lang") == "zh-Hant") {
      return Math.floor(secondsAgo / 86400) + "天前";
    } else {
      return Math.floor(secondsAgo / 86400) + "Days ago";
    }
  } else if (secondsAgo < 31536000) {
    // 365天
    if (localStorage.getItem("lang") == "zh-Hans") {
      return Math.floor(secondsAgo / 2592000) + "个月前";
    } else if (localStorage.getItem("lang") == "zh-Hant") {
      return Math.floor(secondsAgo / 2592000) + "個月前";
    } else {
      return Math.floor(secondsAgo / 2592000) + "Months ago";
    }
  } else {
    if (localStorage.getItem("lang") == "zh-Hans") {
      return Math.floor(secondsAgo / 31536000) + "年前";
    } else if (localStorage.getItem("lang") == "zh-Hant") {
      return Math.floor(secondsAgo / 31536000) + "年前";
    } else {
      return Math.floor(secondsAgo / 31536000) + "last year";
    }
  }
};
// 保留前8后8
const formatHideAddr = (v) => {
  return v.substring(0, 8) + "...." + v.substr(v.length - 8);
};
// 截取小数点后9位
const formatNumber = (num) => {
  const str = num.toString();
  const decimalIndex = str.indexOf(".");
  if (decimalIndex !== -1) {
    return str.slice(0, decimalIndex + 9);
  } else {
    return str;
  }
};
const formatindex = (num) => {
  const result = ethers.utils.formatEther(num);
  const str = result.toString();
  const decimalIndex = str.indexOf(".");
  if (decimalIndex !== -1) {
    return str.slice(0, decimalIndex + 10);
  } else {
    return str;
  }
};
function PowerAndTruncate(value) {
  // console.log(value);
  if (!value || typeof value !== "string" || value.trim() === "") {
    return "0.000";
  }
  const parsedValue = ethers.utils.parseUnits(value, 0);
  const formattedResult = ethers.utils.formatUnits(parsedValue, 18);
  return formattedResult;
}
function PowerAndTruncates(value) {
  // console.log(value);
  if (!value || typeof value !== "string" || value.trim() === "") {
    return "0.000";
  }
  const parsedValue = ethers.utils.parseUnits(value, 0);
  const formattedResult = ethers.utils.formatUnits(parsedValue, 9);
  return formattedResult;
}
// 截取小数点后两位
const formattwo = (num) => {
  const str = num.toString();
  const decimalIndex = str.indexOf(".");
  if (decimalIndex !== -1) {
    return str.slice(0, decimalIndex + 3);
  } else {
    return str;
  }
};
export {
  formatHideAddr,
  formatNumber,
  formatTimeFromTimestamp,
  PowerAndTruncate,
  timeStamp2String,
  PowerAndTruncates,
};
